// pages/my/my.js
var app = getApp();
Page({
  data: {
    // userInfo: {},
    // userName: '',
    //不使用授权的用户民昵称，而是使用
    defaultAvatar: "../../images/user-avater2.png",
    // imageUrl: app.imageUrl
    imageUrl:"../../images/custom.png",
    is_login:'',
    login_nick: ''

  },
  onLoad: function (options) {
    let that = this;
    that.setData({
      is_login: app.globalData.is_login,
      login_nick: app.globalData.login_nick,
      username:'',
      password:''
    });
    // console.log(JSON.stringify(this.data));



  },
  onReady: function () {
    // 页面渲染完成
  },
  onShow: function () {
    var that = this;
    that.setData({
      // userName: wx.getStorageSync('nickname'),
      // userInfo: app.userInfo
    });
  },
  add_dev:function(){
    wx.navigateTo({
      url: '../add_dev/add_dev',
    })
  },

  show_list:function(){
    wx.navigateTo({
      url: '../new_dev_list/new_dev_list',
    });

  },

  onPullDownRefresh: function () {
    var that = this;
    that.setData({
      userName: wx.getStorageSync('userName'),
      userInfo: app.userInfo
    });
    wx.stopPullDownRefresh();
  },
  //关于我们
  abountUs: function () {
    wx.navigateTo({ url: '../about/about' })
  },
  //智能照明监控平台介绍
  system: function () {
    wx.navigateTo({ url: '../system/system' })
  },

//开关灯模板
  onoff_manager:function(){
    wx.navigateTo({
      url: '../onoff_manager/onoff_manager',
    })

  },
  //帮助中心
  helpCenter: function () {
    wx.navigateTo({ url: '../helpCenter/helpCenter' })
  },
  //意见反馈
  feedback: function () {
    wx.navigateTo({ url: '../feedback/feedback' })
  },
  //设置
  setUp: function () {
    var that = this;
    wx.openSetting({
      success: (res) => {
        var flag = res.authSetting['scope.userInfo'];
        if (flag) {
          wx.getUserInfo({
            lang: 'zh_CN',
            success: function (res) {
              app.userInfo = res.userInfo;
              that.setData({ userInfo: app.userInfo });
            }
          })
        } else {
          app.userInfo = {};
          that.setData({ userInfo: {} });
        }
      }
    })
  },
  //更换账号
  changeAccount: function () {
    var header = { 'content-type': 'application/x-www-form-urlencoded' }
    wx.request({
      url: app.url + '/xywl/changeAccount.htm',
      data: {},
      method: 'POST',
      header: header,
      success: function (res) {
        var decodingStr = app.ecbDecrypto(res.data)
        var rest = JSON.parse(decodingStr)
        if (rest.res == 0) {
          wx.setStorageSync('userName', '');
          wx.setStorageSync('deptName', '');
          wx.navigateTo({ url: '../login/login' })
        }
      }
    })
  },
  goLogin: function () {
    var that = this;
    if (JSON.stringify(that.data.userInfo) != "{}") {
      wx.navigateTo({ url: '../login/login' })
    } else {
      wx.showModal({
        title: '需要你允许才能正常显示',
        content: '请点击“去设置”并勾选“用户信息”，然后确定即可正常使用',
        confirmText: '去设置',
        showCancel: false,
        success: function (res) {
          if (res.confirm) {
            wx.openSetting({
              success: function (res) {
                var flag = res.authSetting['scope.userInfo'];
                if (flag) {
                  wx.getUserInfo({
                    lang: 'zh_CN',
                    success: function (res) {
                      app.userInfo = res.userInfo;
                      that.setData({ userInfo: app.userInfo });
                      if (that.data.userName == '' || that.data.userName == null) {
                        wx.navigateTo({ url: '../login/login' })
                      }
                    }
                  })
                } else {
                  app.userInfo = {};
                  that.setData({ userInfo: {} });
                }
              }
            })
          }
        }
      })
    }
  },

//推出登录
  quitLogin:function(){
    let  that = this;
    wx.navigateTo({
      url: '../login/login?username=' + app.globalData.login_name,
    })

  },

  //测试分包
  test_subpage:function(){
    
    wx.navigateTo({
      url: '/package2/pages/test_subpage/test_subpage',
    })
  }



})